import 'package:base_flutter/base_flutter.dart';
import 'package:flutter/material.dart';
import 'package:flutter/src/widgets/framework.dart';
import 'package:ls_work/bad_event/ui/bad_event_list_page.dart';
import 'package:ls_work/doctor_advice/add_doctor_advice_view_model.dart';
import 'package:ls_work/res/gaps.dart';
import 'package:ls_work/widgets/common_widget.dart';
import 'package:ls_work/widgets/event_input_widget.dart';

import '../res/colors.dart';
class AddDoctorAdvicePage extends BaseStatefulMvvmWidget{
  @override
  State<StatefulWidget> createState() => _AddDoctorAdviceState();
}

class _AddDoctorAdviceState extends BaseMvvmState<AddDoctorAdviceViewModel,AddDoctorAdvicePage>{
  Widget get arrowRight => Icon(
    Icons.keyboard_arrow_right,
    size: 15,
    color: Colours.gray_99,
  );
  @override
  Widget? buildLoadingContentView() {
    return SingleChildScrollView(
      padding: EdgeInsets.symmetric(horizontal: 16),
      child: Column(
        children: [
          ...[
            TitleValueWidget(
                title: '长者姓名',
                value: viewModel.customerName ?? "",
                hint: '请选择',
                isRequired: true,
                contentWeight: -1,
                arrow: arrowRight)
                .onTap(() {
              viewModel.chooseElder();
            }),

            TitleValueWidget(
                title: '医疗设备',
                value: viewModel.equipmentName ?? "",
                hint: '请选择',
                isRequired: false,
                contentWeight: -1,
                arrow: arrowRight)
                .onTap(() {
              viewModel.chooseEquipment();
            }),

            Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                TitleValueWidget(
                    title: '频次',
                    value: viewModel.frequencyName ?? "",
                    hint: '请选择',
                    isRequired: true,
                    contentWeight: -1,
                    arrow: arrowRight)
                    .onTap(() {
                  viewModel.chooseFrequency();
                }),

                if(viewModel.frequency==2)
                  Wrap(
                    crossAxisAlignment: WrapCrossAlignment.start,
                    runAlignment: WrapAlignment.start,
                    spacing: 12,runSpacing: 10,
                    children: List.generate(viewModel.weekList.length, (index){
                      var item = viewModel.weekList[index];
                      return CheckWithText(title: item, onChanged: (value){
                        if(value){
                          viewModel.frequencyRemarkList.add(index.toString());
                        }else{
                          viewModel.frequencyRemarkList.remove(index.toString());
                        }
                        print(viewModel.frequencyRemarkList);
                      },defaultSelected: viewModel.frequencyRemarkList.contains(index.toString()),textStyle: TextStyle(color: Colors.black,fontSize: 15),);
                    }),
                  ),

                if(viewModel.frequency==3)
                  Wrap(
                    crossAxisAlignment: WrapCrossAlignment.start,
                    runAlignment: WrapAlignment.start,
                    spacing: 12,runSpacing: 10,
                    children: List.generate(viewModel.monthList.length, (index){
                      var item = viewModel.monthList[index];
                      return CheckWithText(title: item, onChanged: (value){
                        if(value){
                          viewModel.frequencyRemarkList.add(index.toString());
                        }else{
                          viewModel.frequencyRemarkList.remove(index.toString());
                        }
                        print(viewModel.frequencyRemarkList);
                      },defaultSelected: viewModel.frequencyRemarkList.contains(index.toString()),textStyle: TextStyle(color: Colors.black,fontSize: 15),);
                    }),
                  ),

                if(viewModel.frequency==4)
                  ...[
                    TitleValueWidget(title: '添加每年频率', value: '',hint: '请选择',arrow: arrowRight).onTap((){
                      viewModel.addYearFrequency();
                    }),
                    if(viewModel.frequencyRemarkList.isNotEmpty)
                      ...[
                        SizedBox(height: 10,),
                        Wrap(
                          spacing: 10,runSpacing: 10,
                          children: viewModel.frequencyRemarkList.map((e){
                            return Stack(
                              children: [
                                CommonText(e,textSize: 15,textColor: Colors.blue).addToContainer(
                                  padding: EdgeInsets.symmetric(horizontal: 20,vertical: 10),
                                  decoration: BoxDecoration(
                                      color: Colors.blue.withOpacity(0.2),borderRadius: 4.borderRadius
                                  ),
                                ),
                                Icon(Icons.dangerous,color: Colors.red,size: 16,).setLocation(right: 0,top: 0)
                              ],
                            ).onTap((){
                              setState(() {
                                viewModel.frequencyRemarkList.remove(e);
                              });
                            });
                          }).toList(),
                        ),
                        SizedBox(height: 12,)
                      ]
                  ]

              ],
            ),

            Row(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                CommonText('时间设置',textColor: Colours.gray_66),
                SizedBox(width: 12,),
                Column(
                  children: [
                    ...List.generate(viewModel.happTimes.length, (index){
                      var item = viewModel.happTimes[index];
                      return Row(
                        children: [
                          Column(
                            children: [
                              CommonText(item.startTime,hintText: '开始时间').addToContainer(
                                  width: double.infinity,
                                  decoration: BoxDecoration(color: Colours.bg_color,borderRadius: 4.borderRadius),
                                  padding: EdgeInsets.symmetric(horizontal: 16,vertical: 10)
                              ).onTap((){
                                viewModel.chooseTime(item, true);
                              }),
                              SizedBox(height: 10,),
                              CommonText(item.endTime,hintText: '结束时间').addToContainer(
                                  width: double.infinity,
                                  decoration: BoxDecoration(color: Colours.bg_color,borderRadius: 4.borderRadius),
                                  padding: EdgeInsets.symmetric(horizontal: 16,vertical: 10)
                              ).onTap((){
                                viewModel.chooseTime(item, false);
                              }),
                            ],
                          ).setWeight(1),
                          SizedBox(width: 20,),
                          index==0?Icon(Icons.add_circle_outline,color: Colors.blue,size: 30,).onTap((){
                            viewModel.addHappenTime();
                          }):Icon(Icons.remove_circle_outline,color: Colors.red,size: 30,).onTap((){
                            setState(() {
                              viewModel.happTimes.removeAt(index);
                            });
                          })
                        ],
                      );
                    }).insertWidget(SizedBox(height: 12,))
                  ],
                ).setWeight(1)
              ],
            ).addToContainer(
              margin: EdgeInsets.symmetric(vertical: 12)
            ),

            TitleInputWidget(title: '医嘱说明',onChanged: (str){
              viewModel.remark = str;
            },maxLength: 1000,),

            Row(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                CommonText('适用时间',textColor: Colours.gray_66),
                SizedBox(width: 12,),
                Column(
                  children: [
                    CommonText(viewModel.startDate,hintText: '开始时间').addToContainer(
                        width: double.infinity,
                        decoration: BoxDecoration(color: Colours.bg_color,borderRadius: 4.borderRadius),
                        padding: EdgeInsets.symmetric(horizontal: 16,vertical: 10)
                    ).onTap((){
                      viewModel.chooseUseDate();
                    }),
                    SizedBox(height: 10,),
                    CommonText(viewModel.endDate,hintText: '结束时间').addToContainer(
                        width: double.infinity,
                        decoration: BoxDecoration(color: Colours.bg_color,borderRadius: 4.borderRadius),
                        padding: EdgeInsets.symmetric(horizontal: 16,vertical: 10)
                    ).onTap((){
                      viewModel.chooseUseDate();
                    }),
                  ],
                ).setWeight(1)
              ],
            ).addToContainer(
                margin: EdgeInsets.symmetric(vertical: 12)
            ),

            Column(
              children: [
                TitleValueWidget(title: '消息通知', value: '',hint: '',showArrow: false),
                Wrap(
                  spacing: 12,runSpacing: 12,
                  children: viewModel.notifyTypeList.map((e)=>CheckWithText(title: e.gname??"", onChanged: (value){
                    if(value){
                      viewModel.postIdList.add(e.id.toString());
                    }else{
                      viewModel.postIdList.remove(e.id.toString());
                    }
                  },defaultSelected: viewModel.postIdList.contains(e.id.toString()),textStyle: TextStyle(color: Colours.text_dark),)).toList(),
                )
              ],
            )
            
          ].insertWidget(Gaps.dividerLine),


          SizedBox(height: 20,),

          TextButton(onPressed: (){
            viewModel.submitCreate();
          }, child: CommonText('保存',textColor: Colors.white),style: badEventButtonStyle,),
          SizedBox(height: 20,)

        ],
      ),
    );
  }

  @override
  Widget buildRootView(BuildContext context, Widget loadingContentWidget) {
    return Scaffold(
      appBar: AppBar(title: Text('医嘱录入'),),
      body: loadingContentWidget,
    );
  }

  @override
  AddDoctorAdviceViewModel createViewModel() {
    return AddDoctorAdviceViewModel(context);
  }

  @override
  void onRetryClick() {
    viewModel.initData();
  }

}